<template>
  <el-dialog
    title=""
    :visible.sync="showDialog"
  >
    <el-tree
      ref="tree"
      :check-strictly="true"
      :data="allAuth"
      :props="{label:'name'}"
      node-key="id"
      :default-checked-keys="treeArr"
      show-checkbox
    />
    <div slot="footer">
      <el-button @click="showDialog = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getPermissions } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
import { getRoleDetail, updateAuthRole } from '@/api/setting'
export default {
  data() {
    return {
      showDialog: false,
      userId: '',
      allAuth: [],
      treeArr: []
    }
  },
  methods: {
    async submit() {
      const permIds = this.$refs.tree.getCheckedKeys()
      await updateAuthRole({
        id: this.userId,
        permIds
      })
      this.showDialog = false
      this.$message.success('修改成功')
    },
    // 根据id获取单个角色权限
    async getAuthRoleDetail(id) {
      this.userId = id
      const res = await getRoleDetail(id)
      // console.log(res)
      this.treeArr = res.permIds
    },
    // 获取所有权限
    async getAuthDetail() {
      const res = await getPermissions()
      this.allAuth = tranListToTreeData(res, '0')
      // console.log(this.allAuth)
    }
  }
}
</script>

<style>

</style>
